# -- coding: utf-8 --

'''
生成器
'''
# n = 1024 * 1024
#
#
# generator = (i for i in range(n))
# # for item in generator:
# #     print(item)
#
# while True:
#     try:
#         item = next(generator)
#         print(item)
#     except StopIteration:
#         break
#
# '''
# 稍微复杂的生成器
# 能把2除尽的数才会输出
# '''
#
# generator = (i for i in range(10) if i % 2 == 0)
# for i in generator:
#     print(i)


# def generateOddNumbers(n):
#     for i in range(n):
#         if i % 2 == 1:
#             yield i
#
# generator = generateOddNumbers(10)
# for i in generator:
#     print(i)

'''
注意：包含 yield 关键字的函数被称为生成器函数，调用生成器函数会返回一个生成器。
在上面的例子中，函数 generateOddNumbers(n) 包含 yield 关键字，是一个生成器函数，
它返回一个生成器，该生成器产生从 0 到 n 范围内的奇数。
'''

# file = open(r"../pyIterable/test.txt")
# count = 0
# while True:
#     line = file.readline()
#     if not  line:
#         break
#     words = line.split()
#     for word in words:
#         print(word)
#         count = count + 1
#
# print(count)

# dict = {}
# while True:
#     line = file.readline()
#     if not line:
#         break
#     words = line.split()
#     for word in words:
#         if word in dict:
#             dict[word] +=1
#         else:
#             dict[word] = 1
#
# for word,count in dict.items():
#     print(word,count)

